package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.zabbix.Zabbix;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 监控主机
 * Created by liupan on 2017/3/6.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/graph")
public class GraphController {


    /**
     * 获取监控项id获取图形数据
     * @param itemid 监控项id
     */
    @RequestMapping(value = "/get/{itemid}", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> getGraphByItemId(@PathVariable String itemid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("selectGraphItems", "extend");
        params.put("expandName", true);
        params.put("itemids", itemid);
        Map<String, Object> result = zabbix.call("graph.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return mapList;
    }

    /**
     * 获取主机id获取图形数据
     * @param form  hostid主机id
     */
    @RequestMapping(value = "/getByHostid", method = {RequestMethod.POST})
    public List<Map<String,Object>> getGraphByHostId(@RequestBody Map form) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("selectGraphItems", "extend");

        String[] selectItems = new String[]{"itemid","name","units","value_type"};
        params.put("selectItems", selectItems);
        params.put("expandName", true);
        params.put("selectHosts", true);
        params.put("hostids", form.get("hostid").toString().split(","));
        Map<String, Object> result = zabbix.call("graph.get", params);
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        for (Map<String, Object> item : mapList) {
            item.put("id", item.get("graphid"));
            item.put("state", "closed");
            item.put("children", new ArrayList<>()); // 监控项
            item.put("type", 4); // 主机类型属性
        }
        list.addAll(mapList);

        params.clear();
        params.put("application", "general");
        params.put("output", "extend");
        params.put("hostids", form.get("hostid").toString().split(","));

        String[] filter = new String[]{"itemid","name","units","delay","delay_flex"};
        params.put("filter", filter);

        result = zabbix.call("item.get",params);
        mapList = (List<Map<String, Object>>) result.get("result");
        for (Map<String, Object> item : mapList) {
            item.put("id", item.get("itemid"));
            item.put("state", "closed");
            item.put("type", 5); // 主机类型属性
        }
        list.addAll(mapList);
        return list;
    }

    @RequestMapping(value = "/getHostGraph/{hostid}/{graphname}", method = {RequestMethod.GET})
    public List<Map<String, Object>> getHostGraph(@PathVariable String hostid,@PathVariable String graphname) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("selectGraphItems", "extend");
        params.put("expandName", true);
        params.put("hostids", hostid.split(","));
        Map<String, Object> result = zabbix.call("graph.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        for (Map<String, Object> item : mapList) {
            item.put("id", item.get("graphid"));
            item.put("state", "closed");
            item.put("children", new ArrayList<>()); // 监控项
            item.put("type", 4); // 主机类型属性
        }
        return mapList;

    }


    /**
     * 获取图形id获取图形数据
     * @param graphid 图形id
     */
    @RequestMapping(value = "/getByGraphid/{graphid}", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> getGraphById(@PathVariable String graphid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("selectGraphItems", "extend");
        params.put("expandName", true);
        params.put("graphids", graphid);
        Map<String, Object> result = zabbix.call("graph.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return mapList;
    }

    /**
     * 批量删除图形信息
     * @return
     */
    @RequestMapping(value = "/delete/ids", method = {RequestMethod.POST, RequestMethod.GET})
    public List<String> deleteByIds(@RequestParam("ids") String ids){
        Zabbix zabbix = Zabbix.getZabbix();
        if(ids != null && ids.length() > 0) {
            String[] idArr = ids.trim().split(",");
            Map<String, Object> resp = zabbix.call("graph.delete", Arrays.asList(idArr));
            Map<String, Object> result = (Map<String, Object>) resp.get("result");
            List<String> res = (List<String>) result.get("graphids");
            return res;
        }
        return null;
    }
}
